package dingding.使用线程池;

import java.util.concurrent.*;

public class RunAsync {

    private final static int AVALABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

    private final static ThreadPoolExecutor pool = new ThreadPoolExecutor(AVALABLE_PROCESSORS,
            AVALABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES,
            new LinkedBlockingDeque<>(5),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public static void runAsyncWithBizExecutor() throws ExecutionException, InterruptedException {
        CompletableFuture future = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("over");

        }, pool);
        System.out.println(future.get());
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        runAsyncWithBizExecutor();
    }
}
